package mazeMultiThread;

import java.util.Random;

/**
 * Special points that hold a direction used to implement a recursive function in an iterative way
 * @author Dani
 *
 */
//truly random point
public class Point {
	//initial coordinates
	private int x;
	private int y;
	//initial direction and current direction
	private int startDir;
	private int dir;
	//current array of random directions to go + index to iterate through them
	private int dirListIndex = 0;
	private int[] dirList = new int[4];
	//all permutations of four elements
	private static int[] permSet = {0,1,2,3,
									0,1,3,2,
									0,2,1,3,
									0,2,3,1,
									0,3,1,2,
									0,3,2,1,
									1,0,2,3,
									1,0,3,2,
									1,2,0,3,
									1,2,3,0,
									1,3,0,2,
									1,3,2,0,
									2,0,1,3,
									2,0,3,1,
									2,1,0,3,
									2,1,3,0,
									2,3,0,1,
									2,3,1,0,
									3,0,1,2,
									3,0,2,1,
									3,1,0,2,
									3,1,2,0,
									3,2,0,1,
									3,2,1,0};
	
	/**
	 * Builds a point in a (x,y) position with a random direction given by the rnd random number generator
	 * @param x x coordinate
	 * @param y y coordinate
	 * @param rnd random number generator used in choosing the random directions of the point
	 */
	public Point(int x, int y, Random rnd) {
		this.x = x;
		this.y = y;
		int perm;
		
		//Usar cadencia personalizada. El laberinto tenderá a generarse con ramas en unas direcciones semialeatorias
		if (GH.USE_CUSTOM_CADENCE) {
			double prob = rnd.nextDouble()*GH.TOTAL_CADENCE;
			perm = rnd.nextInt(6);
			if (prob < GH.CADENCE[0]) {
				perm += 0;
			} else if (prob < GH.CADENCE[1] + GH.CADENCE[0]) {
				perm += 6;
			} else if (prob < GH.CADENCE[2] + GH.CADENCE[1] + GH.CADENCE[0]) {
				perm += 12;
			} else { //LEFT CADENCE
				perm += 18;
			}
			perm *= 4;
		} else {
			perm = rnd.nextInt(24) *4;
		}

		this.dirList[0] = permSet[perm];
		this.dirList[1] = permSet[perm+1];
		this.dirList[2] = permSet[perm+2];
		this.dirList[3] = permSet[perm+3];
		this.startDir = dirList[0];
		this.dir = dirList[0];
	}
	
	/**
	 * 
	 * @return x coordinate
	 */
	public int getX() {
		return this.x;
	}
	/**
	 * 
	 * @return y coordinate
	 */
	public int getY() {
		return this.y;
	}
	/**
	 * 
	 * @return current point direction (kinda like a vector)
	 */
	public int getDir() {
		return this.dir;
	}
	/**
	 * 
	 * @return starting point direction
	 */
	public int getStartDir() {
		return this.startDir;
	}
	/**
	 * sets the point direction to the next random direction that has not been chosen (or loops around if all have been chosen) and returns the new direction
	 * @return the new direction
	 */
	public int nextDir() {
		this.dirListIndex++;
		return this.dir = this.dirList[dirListIndex%4];
	}
}

//previous class for when generation was recursive

/*
 * package mazeBueno2;

public class Point {
	private int x;
	private int y;
	private int startDir;
	private int dir;
	
	public Point(int x, int y,int startDir) {
		this.x = x;
		this.y = y;
		this.startDir = startDir;
		this.dir = startDir;
	}
	
	public int getX() {
		return this.x;
	}
	public int getY() {
		return this.y;
	}
	public int getDir() {
		return this.dir;
	}
	public int getStartDir() {
		return this.startDir;
	}

	public int nextDir() {
		this.dir = (this.dir+3)%4;
		return this.dir;
	}
	
}*/
